<?php
require_once dirname(__FILE__).'/ImportBase.php';
class ImportForum extends ImportBase{

	private static $instance;

    public static function &get_instance() {
        if (!self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

	/**
	*
	*/
	public function get_dz_forums_info($fids){
		if(!is_array($fids))return false;
		$fids=implode($fids,',');
		//板块ID 上级板块ID 版块名 版块描述 版主
		$stmt=$this->dz_pdo->prepare("select a.fid,a.fup,a.name,b.description,b.moderators from cdb_forums as a left join cdb_forumfields as b on a.fid=b.fid where a.fid in ($fids)");
		$stmt->execute();
		return $stmt->fetchAll();
	}

	private function add_one_forum($name,$description){

		if(!$name)return false;

		$display_order=0;
		$creator_id=0;
		$type=0;
		$status=1;
		$flags=0;
		$creation=date("Y-m-d H:i:s");
		$updated=date("Y-m-d H:i:s");
		$sql="insert into forums set name=?,description=?,display_order=?,creator_id=?,type=?,status=?,flags=?,creation=?,updated=?";
		$stmt = $this->bbs_pdo->prepare($sql);
        $stmt->execute(array($name,$description,$display_order,$creator_id,$type,$status,$flags,$creation,$updated));
		$bbs_forum_id=$this->bbs_pdo->lastInsertId();
		if($bbs_forum_id){
			$sql="insert into forums_ext set id=?,threads=0,digest_threads=0,posts=0,todayposts=0,lastpost=''";
			$stmt = $this->bbs_pdo->prepare($sql);
			$stmt->execute(array($bbs_forum_id));
		}
		return $bbs_forum_id;
	}

	private function add_map_forum($old_forum_id,$new_forum_id){
		if(!$old_forum_id||!$new_forum_id)return false;
		$sql="insert into map_forum set old_forum_id=?,new_forum_id=?";
		$stmt = $this->bbs_pdo->prepare($sql);
		$stmt->execute(array($old_forum_id,$new_forum_id));
	}

	public function get_map_forum($old_forum_id){
		if(!$old_forum_id)return false;
		$sql="select * from  map_forum where old_forum_id=?";
		$stmt = $this->bbs_pdo->prepare($sql);
		$stmt->execute(array($old_forum_id));
		return $stmt->fetch();
	}

	//版主导入
	private function add_forum_moderators($forum_id,$user_ids){
		if(!is_array($user_ids))return false;
		$sql="insert into forum_moderators set moderator_id=?,forum_id=?";
		$stmt = $this->bbs_pdo->prepare($sql);
		foreach($user_ids as $v){
			if($user_ids>0){
				$stmt->execute(array($v,$forum_id));
			}
		}
	}

	public function run($fids){

		$dz_forums_info=$this->get_dz_forums_info($fids);
		if($dz_forums_info){
			foreach($dz_forums_info as $v){
				if($this->get_map_forum($v['fid'])){
					$this->print_msg("版块<".$v['name'].">ID：".$v['fid']."已导入");
					continue;
				}
				$moderators=explode("\t",$v['moderators']);
				$forum_id=$this->add_one_forum($v['name'],$v['description']);
				if($forum_id&&count($moderators)>0){
					foreach($moderators as $moderatorsv){
						$tmp_userid=$this->get_user_id($moderatorsv);
						if($tmp_userid)$user_ids[]=$tmp_userid;
					}
					$this->add_forum_moderators($forum_id,$user_ids);
					unset($user_ids);
					$this->add_map_forum($v['fid'],$forum_id);
				}
				$this->print_msg("版块<".$v['name'].">初始ID：".$v['fid']."新ID：".$forum_id);
			}
		}
	}
}

$fids=array(28);
ImportForum::get_instance()->run($fids);

